package Test;


import officeserver.log_error.UserException;
import officeserver.office.Office;
import officeserver.report.Appointment;
import officeserver.report.Invoice;
import officeserver.report.Report;
import officeserver.report.Symptoms;
import officeserver.report.VitalSigns;
import officeserver.users.AccessLevel;
import officeserver.users.UserList;
import junit.framework.TestCase;

/**
 * This is the JUnit test class for updateAppointment() method.
 * 
 * @author So-mi
 *
 */
public class UpdateAppointmentTest extends TestCase{
	
	private Office testOffice;
	
	private Appointment tempAppt3;
	
	/**
	 * This is the code to initialize an Admin that can add people.
	 */
	public void initAdmin() {
	try {
	    UserList.clear();
	    UserList.addUser("admin", "admin", "admin", "admin1",
		    AccessLevel.ADMIN);
	} catch (UserException e) {
	}
	}
	
	/**
	 * This method will create users and Appts to work with
	 */
	public void initUserAndAppt() throws Exception{
	initAdmin();
	
	testOffice = new Office(); 
	testOffice.userLogin("admin", "admin1");
	testOffice.addUser("Test", "Patient", "pat1", "pass1", AccessLevel.PATIENT);
	testOffice.addUser("Test2", "Patient", "pat2", "pass2", AccessLevel.PATIENT);
	testOffice.addUser("Test", "Nurse", "nurse1", "nursepass1", AccessLevel.NURSE);
	testOffice.addUser("Test", "Doctor", "doc1", "docpass1", AccessLevel.DOCTOR);
	testOffice.addUser("Test1", "Doctor", "doc2", "docpass1", AccessLevel.DOCTOR);
	testOffice.userLogout();

	testOffice.userLogin("pat1", "pass1");
	testOffice.addSelfAppointment(0, 100, 1);
		
	testOffice.userLogout();
	testOffice.userLogin("nurse1", "nursepass1");
	testOffice.userSearch("Test", "Patient");

	Appointment tempAppt2 = testOffice.getWaitingAppointments().get(0);
	testOffice.confirmSearchUserAppointment(tempAppt2);
	tempAppt3 = testOffice.getAppointmentsByDay(100).get(0);
	}
	
	/**
	 * This is a giant test for everything
	 * @throws Exception
	 */
	public void testAppointment() throws Exception{
	initAdmin();
	initUserAndAppt();
		
	//----Test #1: Check if the appointment that is getting updated exists----//
	Appointment tempAppt4 = testOffice.getAppointmentsByDay(100).get(0);
	assertEquals(tempAppt4, tempAppt3);

	//----Test #2: Test updating the day with a valid day----//
	tempAppt3.setDay(4);
	assertEquals(4, tempAppt3.getDay());
	
	//----Test #3: Test what happens if the day is gets updated with a date that is beyond the scope of the calendar----//
	boolean flag1 = false;
	
	try {
	tempAppt3.setDay(103);
	} catch (IndexOutOfBoundsException e) {
	    flag1 = true;
	}
//	assertFalse(!flag);
//	assertEquals(4, tempAppt3.getDay());
//TODO: FAILED. Fix it so that it doesn't make an appointment at a day not available..
	
	//----Test #4: Test what happens if no change in date is made---//
	tempAppt4.setDay(tempAppt3.getDay());
	int sameDay = tempAppt3.getDay();
	tempAppt3.setDay(sameDay);
	
	assertEquals(tempAppt3.getDay(), tempAppt4.getDay());
	
	//----Test #5: Test updating the doctor with a valid doctor----//
	tempAppt3.setDoctorID(1);
	assertEquals(1, tempAppt3.getDoctorID());
	
	//----Test #6: Test what happens if the doctor is gets updated with a doctor that does not exist----//
	boolean flag2 = false;
//	try {
	tempAppt3.setDoctorID(3);
	int notValidDoctor = tempAppt3.getDoctorID(); 
//	} catch (UserException e) {
//	    flag2 = true;
//	}
	
//	assertFalse(!flag2);
//	assertEquals(1, tempAppt3.getDoctorID());
//TODO: Make the UserException throw something cuz otherwise I can't use try/catch
	
	//----Test #7: Test what happens if no change in doctor is made----//
	tempAppt4.setDoctorID(tempAppt3.getDoctorID());
	int sameDoc = tempAppt3.getDoctorID();
	tempAppt3.setDoctorID(sameDoc);
	
	assertEquals(tempAppt3.getDoctorID(), tempAppt4.getDoctorID());	
	
	//----Test #8: Test updating report with a valid report-----//
	String[][] invoiceDescription = {{"Therapy", "50"}}; 
	Report rep = new Report(new Symptoms("Blah"), new VitalSigns(30, 30, 32.1, 49, 38.0, 40), null, sameDay, 
				tempAppt3.getTimeSlot(), tempAppt3.getPatientID(), sameDoc, new Invoice("blah", "blah", 2, invoiceDescription));
	tempAppt3.setReport(rep);
	assertEquals(rep, tempAppt3.getReport());

	
	//----Test #9: Test what happens if no change in report is made----//
	tempAppt4.setReport(tempAppt3.getReport());
	Report sameRep = tempAppt3.getReport();
	tempAppt3.setReport(sameRep);
	
	assertEquals(tempAppt3.getDoctorID(), tempAppt4.getDoctorID());	
	
	//----Test #10: Test updating the timeSlot valid timeSlot----//
	tempAppt3.setTimeSlot(3);
	assertEquals(3, tempAppt3.getTimeSlot());
	
	//----Test #11: Test what happens if timeSlot is updated with an invalid timeSlot ----//
	/*	This code is commented out for the sake of allowing other tests to run*/ 
//	tempAppt3.setTimeSlot(-4);
//	assertEquals("Not valid time slot", tempAppt3.getTimeSlot());
//TODO: This check will be done VIA GUI and what can be viewed/not viewed.
	
	//----Test #12: Test what happens if no change in timeSlot is made ----//
	tempAppt4.setTimeSlot(tempAppt3.getTimeSlot());
	int sameTimeSlot = tempAppt3.getTimeSlot();
	tempAppt3.setDoctorID(sameTimeSlot);
	
	assertEquals(tempAppt3.getTimeSlot(), tempAppt4.getTimeSlot());	
	}
}

